﻿using System.ServiceModel;
using System.ServiceModel.Web;
using Interscape.TwilioClient.Components;
using Interscape.TwilioClient.ServiceHelpers;

namespace Interscape.TwilioClient
{

    #region Service Interface

    [ServiceContract]
    public interface INotificationManager
    {
        [OperationContract]
        [TwilioWcfSerializer]
        [WebGet(
            BodyStyle = WebMessageBodyStyle.Bare,
            ResponseFormat = WebMessageFormat.Xml,
            UriTemplate = "/Accounts/{accountSid}/Notifications"
            )]
        NotificationList GetAll(string accountSid);

        [OperationContract]
        [TwilioWcfSerializer]
        [WebGet(
            BodyStyle = WebMessageBodyStyle.Bare,
            ResponseFormat = WebMessageFormat.Xml,
            UriTemplate = "/Accounts/{accountSid}/Notifications?Log={log}&MessageDate={messageDate}"
            )]
        NotificationList GetAllFiltered(string accountSid, int log, string messageDate);

        [OperationContract]
        [TwilioWcfSerializer]
        [WebGet(
            BodyStyle = WebMessageBodyStyle.Bare,
            ResponseFormat = WebMessageFormat.Xml,
            UriTemplate = "/Accounts/{accountSid}/Notifications/{notificationSid}"
            )]
        Notification Get(string accountSid, string notificationSid);

        [OperationContract]
        [WebInvoke(
            Method = "DELETE",
            BodyStyle = WebMessageBodyStyle.Bare,
            ResponseFormat = WebMessageFormat.Xml,
            UriTemplate = "/Accounts/{accountSid}/Notifications/{notificationSid}"
            )]
        Notification Delete(string accountSid, string notificationSid);

    }

    #endregion

    public class NotificationManager : ManagerBase<INotificationManager>, INotificationManager
    {

        #region Constructors

        /// <summary>
        /// Creates a new instance of the CallManager.
        /// </summary>
        /// <param name="accountSid">The 34 character string that uniquely identifies the Account which will be used to make the TwilioAPI requests.</param>
        /// <param name="authToken">The "password" for your Twilio API calls.</param>
        public NotificationManager(string accountSid, string authToken) : base(accountSid, authToken)
        {
        }

        #endregion

        /// <summary>
        /// Gets a PagedList of all Notifications
        /// </summary>
        /// <returns></returns>
        /// <remarks>
        ///The list is sorted by DateUpdated, with newest notifications first. 
        /// </remarks>
        public NotificationList GetAll()
        {
            return this.GetAll(this.AccountSid);
        }

        /// <summary>
        /// Gets a PagedList of all Notifications
        /// </summary>
        /// <param name="accountSid">A 34 character string that uniquely identifies the Account associated with this resource.</param>
        /// <returns></returns>
        /// <remarks>
        ///The list is sorted by DateUpdated, with newest notifications first. 
        /// </remarks>
        public NotificationList GetAll(string accountSid)
        {
            return this.Channel.GetAll(accountSid);
        }

        /// <summary>
        /// Gets a PagedList of Notifications for a given type or date. 
        /// </summary>
        /// <param name="log">0 for Errors, 1 for Warnings</param>
        /// <param name="messageDate">A date in the format YYYY-MM-DD</param>
        /// <returns></returns>
        public NotificationList GetAllFiltered(int log, string messageDate)
        {
            return this.GetAllFiltered(this.AccountSid, log, messageDate);
        }

        /// <summary>
        /// Gets a PagedList of Notifications for a given type or date. 
        /// </summary>
        /// <param name="accountSid"></param>
        /// <param name="log">0 for Errors, 1 for Warnings</param>
        /// <param name="messageDate">A date in the format YYYY-MM-DD</param>
        /// <returns></returns>
        public NotificationList GetAllFiltered(string accountSid, int log, string messageDate)
        {
            return this.Channel.GetAllFiltered(accountSid, log, messageDate);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="notificationSid"></param>
        /// <returns></returns>
        public Notification Get(string notificationSid)
        {
            return this.Get(this.AccountSid, notificationSid);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="accountSid">A 34 character string that uniquely identifies the Account associated with this resource.</param>
        /// <param name="notificationSid"></param>
        /// <returns></returns>
        public Notification Get(string accountSid, string notificationSid)
        {
            return this.Channel.Get(accountSid, notificationSid);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="notificationSid"></param>
        /// <returns></returns>
        public Notification Delete(string notificationSid)
        {
            return this.Delete(this.AccountSid, notificationSid);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="accountSid">A 34 character string that uniquely identifies the Account associated with this resource.</param>
        /// <param name="notificationSid"></param>
        /// <returns></returns>
        public Notification Delete(string accountSid, string notificationSid)
        {
            return this.Channel.Delete(accountSid, notificationSid);
        }

    }
}
